package Server;

import Data.GroupData;
import Data.GroupFriendData;
import Data.event;
import MyInterfaces.MassageType;

import java.io.*;
import java.net.Socket;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;
import java.util.Objects;


public class ChangeGroupInformation {
    //修改群资料
    static String url = "jdbc:mysql://127.0.0.1:3306/nox?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";
    static String user = "root";
    static String password = "Mi20030610.";

    private GroupData groupData;

    public boolean change() throws IOException, SQLException {
        String sql="update chat_room_list set ";
        String path="F:\\qqserverfile\\group_avatar";
        Connection Conn = DriverManager.getConnection(url, user, password);
        ArrayList<String> arry=new ArrayList<>();
        if(groupData.getGroup_avatar()!=null)
        {
            //如果修改了
            path=path+"\\"+groupData.getGroup_id()+"."+groupData.getSuffix_name();
            File file=new File(path);
            FileOutputStream fileOutputStream=new FileOutputStream(file);
            fileOutputStream.write(groupData.getGroup_avatar());
            fileOutputStream.close();
            sql=sql+"group_avatar=?,";
            arry.add(path.replace("\\","\\\\"));
        }

        if(groupData.getGroup_name()!=null)
        {
            sql=sql+"name=?,";
            arry.add(groupData.getGroup_name());
        }

        if(groupData.getGroup_saying()!=null)
        {
            sql=sql+"group_saying=?,";
            arry.add(groupData.getGroup_saying());
        }
        if(groupData.getCreater_id()!=null)
        {
            sql=sql+"creater_id=?,";
            arry.add(groupData.getCreater_id());
        }

        if(sql.charAt(sql.length()-1)==',')
        {
            sql=sql.substring(0,sql.length()-1);
        }
        sql=sql+"where group_id=?;";
        arry.add(groupData.getGroup_id());

        PreparedStatement operatiion=Conn.prepareStatement(sql);
        for (int i = 0; i < arry.size(); i++) {
            operatiion.setString(i+1,arry.get(i));
        }
        System.out.println(sql);

        int k=operatiion.executeUpdate();
        if(k<=0) return false;
        ArrayList<GroupFriendData> arrayList=groupData.getGroup_friend_list();
        //遍历群聊列表去修改所有人的界面
        groupData.setGroup_friend_list(null);
        for (int i = 0; i < arrayList.size(); i++) {
            if(arrayList.get(i).getId().equals(groupData.getId()))
            {
                //如果是自己就跳过吧
            }
            else if(ManageClientConnectionServerThread.existId(arrayList.get(i).getId()))
            {
                Socket socket= ManageClientConnectionServerThread.getSocketByUserid(arrayList.get(i).getId());
                //其他人返回应该change_Group_information的消息吧
                event returnmassage=new event();
                ObjectOutputStream writer=new ObjectOutputStream(socket.getOutputStream());
                returnmassage.setType(MassageType.Change_Group_Information);
                returnmassage.setT(groupData);

                writer.writeObject(returnmassage);
                writer.flush();
            }
        }
        return true;
    }

    public static void setUrl(String url) {
        ChangeGroupInformation.url = url;
    }

    public ChangeGroupInformation(GroupData groupData) {
        this.groupData = groupData;
    }



}
